package org.ielts.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.ielts.domain.base.Course;
import org.ielts.domain.base.Student;
import org.ielts.domain.log.ConsultLog;
import org.ielts.domain.log.SignCourseLog;
import org.ielts.domain.report.ConsultantSignedReport;
import org.ielts.domain.security.User;
import org.ielts.repository.AppointConsultorLogRepository;
import org.ielts.repository.ConsultLogRepository;
import org.ielts.repository.CourseRepository;
import org.ielts.repository.SignCourseLogRepository;
import org.ielts.repository.StudentRepository;
import org.ielts.repository.security.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yifeng.ProfileContext;
import com.yifeng.profile.Profile;

@Service
@Transactional
public class ConsultantService {

	@Autowired
	CourseRepository courseRepository;

	@Autowired
	ConsultLogRepository consultLogRepository;

	public ConsultLog consult(String id, String source, String title, String courseId) {

		Student student = studentRepository.findOne(id);

		ConsultLog consultLog = new ConsultLog();
		consultLog.setCreateTime(new Date());

		if (StringUtils.isNotBlank(courseId)) {
			Course course = courseRepository.findOne(courseId);
			consultLog.setCourse(course);
		}

		User user = (User) Profile.getInstance().getUser();
		consultLog.setCreator(user);

		consultLog.setStudent(student);
		consultLog.setSource(source);
		consultLog.setTitle(title);

		consultLogRepository.save(consultLog);

		return consultLog;
	}

	public SignCourseLog signUp(String studentId, String courseId, String consultantId, String contractId,
			String cardId, String term, int times, Date beginTime, Date endTime) {
		SignCourseLog signCourseLog = new SignCourseLog();

		User creator = (User) Profile.getInstance().getUser();
		signCourseLog.setCreator(creator);

		User consultant = new User();
		consultant.setId(consultantId);
		signCourseLog.setConsultant(consultant);

		Student student = new Student();
		student.setId(studentId);
		signCourseLog.setStudent(student);

		Course course = new Course();
		course.setId(courseId);
		signCourseLog.setCourse(course);

		signCourseLog.setContractId(contractId);
		signCourseLog.setCardId(cardId);
		signCourseLog.setTerm(term);
		signCourseLog.setTimes(times);
		signCourseLog.setBeginTime(beginTime);
		signCourseLog.setEndTime(endTime);

		signCourseLogRepository.save(signCourseLog);

		return signCourseLog;
	}

	@Autowired
	UserRepository userRepository;

	@Autowired
	StudentRepository studentRepository;

	public Iterable<User> listConsultant() {
		String role = "课程顾问";
		Iterable<User> users = userRepository.findByRoleName(role);
		return users;
	}

	public long countStarredStudentByConsultant(String id) {

		long count = studentRepository.countStarredByConsultant(id);

		return count;
	}

	public Iterable<Student> listStarredStudentByConsultant(String id) {
		Iterable<Student> students = studentRepository.findStarredByConsultant(id);
		return students;
	}

	public void starredConsult(String id) {
		Student student = studentRepository.findOne(id);

		student.setConsultStarred(true);
	}

	public void unStarredConsult(String id) {
		Student student = studentRepository.findOne(id);

		student.setConsultStarred(false);
	}

	@Autowired
	AppointConsultorLogRepository appointConsultorLogRepository;

	public long countAssignedByConsultant(String id) {
		long count = appointConsultorLogRepository.countByConsultant(id);

		return count;
	}

	@Autowired
	SignCourseLogRepository signCourseLogRepository;

	public long countSignedByConsultant(String id) {
		long count = signCourseLogRepository.countByConsultant(id);

		return count;
	}

	public Page<Student> listMyStudent(String myid, Pageable pageable) {
		List<Student> sts = studentRepository.findStudentByConsultantId(myid, pageable);
		return new PageImpl<Student>(sts, pageable, studentRepository.countByConsultantId(myid));
	}

	public Map<User, Long> reportStarredStudent() {
		Iterable<User> users = listConsultant();

		final Map<User, Long> reports = new LinkedHashMap<User, Long>();

		for (User user : users) {
			long count = studentRepository.countStarredByConsultant(user.getId());

			reports.put(user, count);
		}

		ArrayList<User> returnUsers = new ArrayList<User>();

		for (User user : users) {
			returnUsers.add(user);
		}

		Collections.sort(returnUsers, new Comparator<User>() {

			public int compare(User o1, User o2) {
				long l1 = reports.get(o1);
				long l2 = reports.get(o2);

				return (int) (l2 - l1);
			}

		});

		return reports;
	}

	public ArrayList<ConsultantSignedReport> reportSignedStudent() {
		Iterable<User> users = listConsultant();

		ArrayList<ConsultantSignedReport> reports = new ArrayList<ConsultantSignedReport>();

		for (User user : users) {
			long assignedCount = countAssignedByConsultant(user.getId());
			long signedCount = countSignedByConsultant(user.getId());

			ConsultantSignedReport consultantSignedReport = new ConsultantSignedReport();
			consultantSignedReport.setAssigned(assignedCount);
			consultantSignedReport.setSigned(signedCount);

			consultantSignedReport.setUser(user);

			reports.add(consultantSignedReport);
		}

		ArrayList<User> returnUsers = new ArrayList<User>();

		for (User user : users) {
			returnUsers.add(user);
		}

		return reports;
	}
}
